package com.qumoyugo.springframework.beans.factory.support;

import com.qumoyugo.springframework.beans.BeansException;
import com.qumoyugo.springframework.beans.factory.BeanFactory;
import com.qumoyugo.springframework.beans.factory.config.BeanDefinition;

/**
 * @author Cedric
 * @date 2021/11/17 15:14
 * @description 抽象Bean工厂
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) throws BeansException {
        Object bean = getSingletonBean(name);
        if (bean != null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition);
    }

    /**
     * 根据name获取Bean定义
     * @param beanName Bean名称
     * @return Bean定义
     * @throws BeansException Beans异常
     */
    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    /**
     * 创建Bean对象
     * @param beanName Bean名称
     * @param beanDefinition Bean定义
     * @return Bean对象
     * @throws BeansException Beans异常
     */
    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;
}
